import {createRouter,createWebHashHistory} from 'vue-router'
import { CONFIG } from '../config/index.js'
import generateRoutes from './generateRoutes.js'
import UserRoutes from './user.js'
import AssetsRoutes from './assets.js'
import knowledgeRoutes from './knowledge.js'
import departmentRoutes from './department.js'
import MenusRoutes from './menus.js'
import RolesRoutes from './roles.js'
// 路由懒加载
// const User= () => import('../views/User.vue')
const Login = () => import('../view/Login.vue')
// const Index = () => import('../view/Index.vue')
const Layout = () => import('../view/layout/Layout.vue')
const LoginAuth0 = () => import('../view/LoginAuth0.vue')
const Auth0Token = () => import('../view/Auth0Token.vue')
// 定义用户相关的路由映射

const listRoutes = {
}
// 定义路由映射
// const routes = [
//     // listRoutes,
//     {
//         path: "/login",
//         component: Login,
//     },
//     {
//         path: "/loginauth0",
//         component: LoginAuth0,
//     },
//     {
//         path: "/",
//         component: Layout
//     },
//     {
//         path: "/oauth/token",
//         component: Auth0Token
//     },
//     UserRoutes,
//     AssetsRoutes,
//     knowledgeRoutes,
//     departmentRoutes,
//     MenusRoutes,
//     RolesRoutes,
// ]
const staticRoutes = [
    // listRoutes,
    {
        path: "/login",
        component: Login,
    },
    {
        path: "/loginauth0",
        component: LoginAuth0,
    },
    {
        path: "/",
        component: Layout
    },
    {
        path: "/oauth/token",
        component: Auth0Token
    }
]
// export async function loadUserRoutes(menuData) {
//   const dynamicRoutes = generateRoutes(menuData)
// //   console.log("======================dynamicRoutes=========20250924",dynamicRoutes)
//   dynamicRoutes.forEach(r => router.addRoute('Layout',r))
  
// }
export async function loadUserRoutes(menuTree) {
  const dynamicRoutes = generateRoutes(menuTree)

  dynamicRoutes.forEach(route => {
    if (route.path.startsWith('/')) {
      // 一级绝对路径
      router.addRoute(route)
    } else {
      // 相对路径，挂在 Layout 下
      router.addRoute('Layout',route)
    }
  })
}
// 创建路由实例
const router = createRouter({
    history: createWebHashHistory(),
    routes: staticRoutes
})
// 定义一个全局的守卫，去判断请求链接有没有Token字段
router.beforeEach((to,from,next)=>{
    // 1、访问的是login,本地没有token ==> next()
    // 2、访问的还是login,本地有了token ==> next("/")
    // 3、访问的不是login,本地有了token ==> next()
    // 4、访问的不是login,本地没有token ==> next("/login")
    // 拿到访问路径
    const toPath = to.path
    const toLogin = toPath.indexOf("/login") // 0 没有找到 -1
    // 判断本地是否有token
    const tokenStatus = window.localStorage.getItem(CONFIG.TOKEN_NAME)
    // const menuData = window.localStorage.getItem(CONFIG.MENUS)
    if (toLogin == 0 && tokenStatus ) {
        next("/")
    } else if (toLogin == 0 && !tokenStatus){
        // 放行
        next()
    } else if ( tokenStatus ) {
        next()
    } else {
        window.localStorage.removeItem(CONFIG.TOKEN_NAME)
        window.localStorage.removeItem(CONFIG.MENUS)
        window.localStorage.removeItem(CONFIG.LOGINTYPE)
        window.localStorage.removeItem(CONFIG.LOGINNAME)
        window.localStorage.removeItem(CONFIG.MD5HASH)
        window.localStorage.removeItem(CONFIG.USERROLUSERROLEINFO)
        next("/login")
    }


})
export default router
